/**
 * There are code generation
 */
package com.neusoft.lohas.modules.ivm.entity.merchant;

import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.hibernate.annotations.Where;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Lists;
import com.neusoft.lohas.common.persistence.IdEntity;
import com.neusoft.lohas.common.utils.DateUtils;
import com.neusoft.lohas.modules.sys.entity.Dict;

/**
 * 收货时间Entity
 * @author Jackie.Liu
 * @version 2014-10-28
 */
@Entity
@Table(name = "ivm_receipt_time")
@DynamicInsert @DynamicUpdate
public class ReceiptTime extends IdEntity<ReceiptTime> {

	private static final long serialVersionUID = 1L;
	private Merchant merchant; 		// 所属商户
	private int timeSlot; 			// 最早收货时间在下几个时间段
	
	private List<TimeSlot> timeSlots = Lists.newArrayList(); // 拥有角色列表

	public ReceiptTime() {
		super();
	}

	public ReceiptTime(String id){
		this();
		this.id = id;
	}

	@OneToOne
	@JoinColumn(name="merchant_id")
	@NotFound(action = NotFoundAction.IGNORE)
	@JsonIgnore
	@NotNull(message="所属商户不能为空")
	public Merchant getMerchant() {
		return merchant;
	}

	public void setMerchant(Merchant merchant) {
		this.merchant = merchant;
	}

	public int getTimeSlot() {
		return timeSlot;
	}

	public void setTimeSlot(int timeSlot) {
		this.timeSlot = timeSlot;
	}

	@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "receiptTime")
	@Where(clause="del_flag='"+DEL_FLAG_NORMAL+"'")
	@OrderBy("sort")
	@NotFound(action = NotFoundAction.IGNORE)
	@JsonIgnore
	public List<TimeSlot> getTimeSlots() {
		return timeSlots;
	}

	public void setTimeSlots(List<TimeSlot> timeSlots) {
		this.timeSlots = timeSlots;
	}

	/**
	 * 封装提交订单用户选择的收货时间列表
	 * @return
	 */
	@Transient
	public List<String[]> getChoiceReceiptTimes(){
		List<String[]> list = Lists.newArrayList();
		if (timeSlots.size()>0) {
			Date mydate=new   Date();   
			int myweekday=mydate.getDay();
			//当前时间是否大于16点
			boolean isAfter = (mydate.getTime()>DateUtils.parseDate(DateUtils.getDate()+" 16:00:00", "yyyy-MM-dd HH:mm:ss").getTime()) ? true : false ;
			if (isAfter) {  //如果超过16点
				if (myweekday==0 || myweekday==6) { //如果是周末
					for (TimeSlot bean : timeSlots) {
						if (bean.getWeekend()==1 && bean.getAfterSixteen()==1) {  //周末可选且16点后可选
							String[] item = new String[2];
							item[0] = bean.getId();
							item[1] = bean.getTimeText();
							list.add(item);
						}
					}
				}else {  //如果是平时，即工作日
					for (TimeSlot bean : timeSlots) {
						if (bean.getUsual()==1 && bean.getAfterSixteen()==1) {  //周末可选且16点后可选
							String[] item = new String[2];
							item[0] = bean.getId();
							item[1] = bean.getTimeText();
							list.add(item);
						}
					}
				}
			}else {
				if (myweekday==0 || myweekday==6) { //如果是周末
					for (TimeSlot bean : timeSlots) {
						if (bean.getWeekend()==1) {  //周末可选
							String[] item = new String[2];
							item[0] = bean.getId();
							item[1] = bean.getTimeText();
							list.add(item);
						}
					}
				}else {  //如果是平时，即工作日
					for (TimeSlot bean : timeSlots) {
						if (bean.getUsual()==1) {  //平时（即工作日）可选
							String[] item = new String[2];
							item[0] = bean.getId();
							item[1] = bean.getTimeText();
							list.add(item);
						}
					}
				}
			}
		}
		return list;
	}
	
	/**
	 * 根据选择的区域
	 * 封装收货时间列表
	 * @return
	 */
	@Transient
	public List<String[]> getReceiptTimesByCounty(String county){
		List<String[]> list = Lists.newArrayList();
		if (timeSlots.size()>0) {
			Date mydate=new   Date();   
			int myweekday=mydate.getDay();
			//当前时间是否大于16点
			boolean isAfter = (mydate.getTime()>DateUtils.parseDate(DateUtils.getDate()+" 16:00:00", "yyyy-MM-dd HH:mm:ss").getTime()) ? true : false ;
			if (isAfter) {  //如果超过16点
				if (myweekday==0 || myweekday==6) { //如果是周末
					for (TimeSlot bean : timeSlots) {
						boolean isContain = false;
						if(bean.getAreaList().size()>0){
							for(Dict area:bean.getAreaList()){
								if(area.getValue().equals(county)){
									isContain = true;
								}
							}
						}
						if(!isContain){
							continue;
						}
						if (bean.getWeekend()==1 && bean.getAfterSixteen()==1) {  //周末可选且16点后可选
							String[] item = new String[2];
							item[0] = bean.getId();
							item[1] = bean.getTimeText();
							list.add(item);
						}
					}
				}else {  //如果是平时，即工作日
					for (TimeSlot bean : timeSlots) {
						boolean isContain = false;
						if(bean.getAreaList().size()>0){
							for(Dict area:bean.getAreaList()){
								if(area.getValue().equals(county)){
									isContain = true;
								}
							}
						}
						if(!isContain){
							continue;
						}
						if (bean.getUsual()==1 && bean.getAfterSixteen()==1) {  //周末可选且16点后可选
							String[] item = new String[2];
							item[0] = bean.getId();
							item[1] = bean.getTimeText();
							list.add(item);
						}
					}
				}
			}else {
				if (myweekday==0 || myweekday==6) { //如果是周末
					for (TimeSlot bean : timeSlots) {
						boolean isContain = false;
						if(bean.getAreaList().size()>0){
							for(Dict area:bean.getAreaList()){
								if(area.getValue().equals(county)){
									isContain = true;
								}
							}
						}
						if(!isContain){
							continue;
						}
						if (bean.getWeekend()==1) {  //周末可选
							String[] item = new String[2];
							item[0] = bean.getId();
							item[1] = bean.getTimeText();
							list.add(item);
						}
					}
				}else {  //如果是平时，即工作日
					for (TimeSlot bean : timeSlots) {
						boolean isContain = false;
						if(bean.getAreaList().size()>0){
							for(Dict area:bean.getAreaList()){
								if(area.getValue().equals(county)){
									isContain = true;
								}
							}
						}
						if(!isContain){
							continue;
						}
						if (bean.getUsual()==1) {  //平时（即工作日）可选
							String[] item = new String[2];
							item[0] = bean.getId();
							item[1] = bean.getTimeText();
							list.add(item);
						}
					}
				}
			}
		}
		return list;
	}
}


